<?php 
/**
 * File Uploader
 * @author Kirill Egorov 2010
 */
class Upload{
    
    protected $_config;
    protected $_uploaders;
    
    public function __construct(array $config){
        $this->_config = $config;
        $this->_uploaders = array();
    }
    /**
     * Auto create dirs for upload
     * @param string $path
     */
    static public function createDirs($root , $path){
     
            $path = str_replace($root.'/', '', $path);
        
            $paths = explode('/', $path);
            $curPath = $root.'/';
            
            foreach ($paths as $k)
            {
                $curPath.=$k . '/';
                
                if(empty($k))
                    continue;
                    
                if(!is_dir($curPath)) {
                    if(!@mkdir($curPath)){
                        return false;
                    }
                    @chmod($curPath, 0775);
                } 
            }
            return true;
    }
    /**
     * Identify filetype
     * @param string $extension
     * @return mixed string / false
     */
    protected function _identifyType($exstension)
    {
        foreach ($this->_config as $k=>$v)
            if(in_array($exstension, $v['extensions']))
                return  $k;
                
        return false;
    }
    
   /**
     * Multiple upload files
     * @property array $data - array of Request::files()  items
     * @param string $path
     * @return mixed  - uploaded files Info / false on error
     */
    public function start(array $files , $path)
    {
            $uploadedFiles = array();
            foreach ($files as $k=>$item) 
            {
                    if($item['error'])
                         continue;
                            
                     $item['name'] = str_replace(' ','_' , $item['name']);
	                 $item['name'] =  preg_replace("/[^A-Za-z0-9_\-\.]/i",'',$item['name']);    

	                 $item['ext'] = File::getExt($item['name']);  
	                 $item['title'] = str_replace($item['ext'],'', $item['name']) ;
                     $type = $this->_identifyType($item['ext']);

                     
                     if(!$type)
                         continue;
             
                     switch ($type)
                     {
                         case 'image' :     if(!isset($this->_uploaders['image']))
                                                             $this->_uploaders['image'] = new Upload_Image($this->_config['image']);
                                                     
                                                      $file = $this->_uploaders['image']->upload($item , $path);
                                                      if(!empty($file)){
                                                              $file['type'] = $type;
	                                                          $file['title'] = $item['title'];
                                                             $uploadedFiles[] =$file;
                                                      }
                                 break;
                             
                         case 'audio':                     
                         case 'video':
                         case 'file':           if(!isset($this->_uploaders['file']))
                                                          $this->_uploaders['file'] = new Upload_File($this->_config[$type]);
                                             
                                                     $file =  $this->_uploaders['file']->upload($item , $path);
                                                      
                                                    if(!empty($file)){
                                                          $file['type'] = $type;
	                                                      $file['title'] = $item['title'];
                                                          $uploadedFiles[] =$file;
                                                    }
                                                             
                                 break;
                     }
            }
            return $uploadedFiles;
    }

}